<script>
	import apis from './apis/apis.js';
	
	export default {
		onLaunch: function() {
			console.log('App Launch')
			// this.autoLogin();
			this.loadConfigData();
		},
		onShow: function() {
			console.log('App Show')
			this.autoLogin();
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			loadConfigData() {
				const hisConfig = uni.getStorageSync('mobileConfig');
				//发起网络请求
				this.$http.get({
					url: apis.mobileConfig,
					data: {},
					noAuth: true,
				}).then(res => {
					if (res.code === 200) {
						if (hisConfig && hisConfig.indexDefaultProvince) {
							res.data.indexDefaultProvince = hisConfig.indexDefaultProvince;
						}
						if (hisConfig && hisConfig.indexDefaultCity) {
							res.data.indexDefaultCity = hisConfig.indexDefaultCity;
						}
						// 缓存
						uni.setStorageSync('mobileConfig', res.data);
					}
				}).catch(err => {
					console.log('调用接口失败：', err);
				});
			},
			autoLogin() {
				const userInfo = uni.getStorageSync('userInfo');
				const that = this;
				if (userInfo && userInfo.openId) {
					//发起网络请求
					that.$http.post({
						url: apis.loginUrl,
						data: {
							username: userInfo.openId
						},
						noAuth: true,
					}).then(res => {
						if (res.code === 200) {
							uni.setStorageSync('token', res.token);
							// 加载用户信息
							that.$http.get({
								url: apis.userInfoUrl
							}).then(res => {
								res.data.loginStatus = true;
								uni.setStorageSync('userInfo', res.data);
							}).catch(err => {
								console.log('调用接口失败：', err);
							})
						}
					}).catch(err => {
						console.log('调用接口失败：', err);
						uni.removeStorageSync('token');
						uni.removeStorageSync('userInfo')
					});
				}
			}
		}
	}
</script>

<style>
	/*每个页面公共css */
	button[type=primary] {
		background-color: #4cbd88;
	}
	
	button[type=primary]:active {
		background-color: #4cbd88;
	}
	
	button[type=primary]:focus {
		background-color: #5ae0a2;
	}
	
	::-webkit-scrollbar {
		display: none;
	}
	
	.tcr-ellipsis {
		white-space: nowrap; /* 不换行 */
		overflow: hidden; /* 隐藏超出部分 */
		text-overflow: ellipsis; /* 显示省略号 */
	}
	
	.tcr-auto-line {
		white-space: normal;
		word-break: break-all;
	}
	
	.tcr-tint-title {
		color: #999999;
	}
	
	.status-bar {
	    height: var(--status-bar-height);
	}
	
	.t-base-card {
		background-color: #ffffff;
		padding: 30rpx;
		border-radius: 20rpx;
	}
	
	.t-base-title {
		font-size: 32rpx;
		color: #333333;
	}
</style>
